package com.study.createobject;

import java.util.Scanner;

public class ShoppingCartTest {
    public static void main(String[] args) {
        // 1.定义商品类，用于后期创建商品对象。
        // 2.定义购物车对象，使用一个数据对象表示。
        Goods[] shoppingCart = new Goods[100];
        while (true) {
            System.out.println("请选择如下命令进行：");
            System.out.println("添加商品到购物车——add");
            System.out.println("查询商品到购物车——query");
            System.out.println("修改商品数量——update");
            System.out.println("结算商品金额——pay");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入命令：");
            String command = sc.next();
            switch (command){
                case "add":
                    //添加商品到购物车
                    addGoods(shoppingCart,sc);
                break;
                case "query":
                    //查询商品到购物车
                    queryGoods(shoppingCart);
                    break;
                case "update":
                    //修改商品数量
                    updateGoods(shoppingCart,sc);
                    break;
                case "pay":
                    //结算商品金额
                    payGoods(shoppingCart);
                    break;
                default:
                    System.out.println("无该功能！");
            }
        }

    }

    /**
     * 购物车结算
     * @param shoppingCart
     */
    private static void payGoods(Goods[] shoppingCart) {
        queryGoods(shoppingCart);
        double money = 0;
        for (int i = 0; i < shoppingCart.length; i++) {
            if (shoppingCart[i] != null){
                Goods g = shoppingCart[i];
                money += g.price *g.buyNumbers;
            }else {
                break;
            }
        }
        System.out.println("购物车结算总金额为："+money);
    }

    /**
     * 用户输入要修改的商品的id，根据ID修改商品
     * @param shoppingCart
     */
    private static void updateGoods(Goods[] shoppingCart,Scanner sc) {
        while (true) {
            System.out.println("请输入要修改的商品Id");
            int id = sc.nextInt();
            Goods g = getGoodsById(shoppingCart, id);
            if (g == null){
                System.out.println("对不起！你没有购买该商品！");
            }else {
                System.out.println("请输入"+g.name+"的数量");
                g.buyNumbers = sc.nextInt();
                System.out.println("修改完成！");
                queryGoods(shoppingCart);
                break;
            }
        }
    }

    public static Goods getGoodsById(Goods[] shoppingCart,int id){
        for (int i = 0; i < shoppingCart.length; i++) {
            if (shoppingCart[i]!=null && shoppingCart[i].id == id){
                return  shoppingCart[i];
            }else if (shoppingCart[i] == null){
                break;
            }
        }
        return null;
    }
    /*
    * 查询购物车的商品信息并展示出来
    * */
    private static void queryGoods(Goods[] shoppingCart) {
        if (shoppingCart[0]==null){
            System.out.println("购物车里还没有添加商品！");
        }else {
            System.out.println("==================查询购物车信息如下====================");
            System.out.println("编号\t\t名称\t\t\t\t\t价格\t\t\t\t购买数量");

            for (int i = 0; i < shoppingCart.length; i++) {
                Goods g = shoppingCart[i];
                if (shoppingCart[i] == null){
                    break;
                }
                System.out.println(g.id+"\t\t"+g.name+"\t\t\t"+g.price+"\t\t\t"+g.buyNumbers);
            }
        }

    }
    /*
    * 完成商品添加到购物车的功能
    * */
    private static void addGoods(Goods[] shoppingCart,Scanner sc) {
    // 1.录入用户输入的购买商品的信息
        Goods goods = new Goods();
        //  输入购买商品的id
        System.out.println("请输入购买商品的id（不重复）");
        goods.id = sc.nextInt();
        //  输入购买商品的名称
        System.out.println("请输入购买商品的名称");
        goods.name = sc.next();
        //  输入购买商品的数量
        System.out.println("请输入购买商品的数量");
        goods.buyNumbers = sc.nextInt();
        //  输入购买商品的价格
        System.out.println("请输入购买商品的价格");
        goods.price = sc.nextDouble();
        //  添加到购物车
        for (int i = 0; i < shoppingCart.length; i++) {
            if (shoppingCart[i] == null){
                shoppingCart[i] = goods;
                System.out.println("你的商品添加购物车已经完成！");
                break;
            }
        }
    }

}
